home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spambayes / hammiebulk.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  6.7 KB  |  246 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Usage: %(program)s [-D|-d] [options]
  5.  
  6. Where:
  7.     -h
  8.         show usage and exit
  9.     -d FILE
  10.         use the DBM store.  A DBM file is larger than the pickle and
  11.         creating it is slower, but loading it is much faster,
  12.         especially for large word databases.  Recommended for use with
  13.         hammiefilter or any procmail-based filter.
  14.         Default filename: %(DEFAULTDB)s
  15.     -p FILE
  16.         use the pickle store.  A pickle is smaller and faster to create,
  17.         but much slower to load.  Recommended for use with sb_server and
  18.         sb_xmlrpcserver.
  19.         Default filename: %(DEFAULTDB)s
  20.     -U
  21.         Untrain instead of train.  The interpretation of -g and -s remains
  22.         the same.
  23.     -f
  24.         run as a filter: read a single message from stdin, add a new
  25.         header, and write it to stdout.  If you want to run from
  26.         procmail, this is your option.
  27.     -g PATH
  28.         mbox or directory of known good messages (non-spam) to train on.
  29.         Can be specified more than once, or use - for stdin.
  30.     -s PATH
  31.         mbox or directory of known spam messages to train on.
  32.         Can be specified more than once, or use - for stdin.
  33.     -u PATH
  34.         mbox of unknown messages.  A ham/spam decision is reported for each.
  35.         Can be specified more than once.
  36.     -r
  37.         reverse the meaning of the check (report ham instead of spam).
  38.         Only meaningful with the -u option.
  39. '''
  40.  
  41. try:
  42.     (True, False)
  43. except NameError:
  44.     (True, False) = (1, 0)
  45.     
  46.     def bool(val):
  47.         return not (not val)
  48.  
  49.  
  50. import sys
  51. import os
  52. import getopt
  53. from spambayes.Options import options, get_pathname_option
  54. from spambayes import classifier, mboxutils, hammie, Corpus, storage
  55. Corpus.Verbose = True
  56. program = sys.argv[0]
  57. if options[('Storage', 'persistent_storage_file')] == options.default('Storage', 'persistent_storage_file'):
  58.     options[('Storage', 'persistent_storage_file')] = os.path.join('~', '.hammiedb')
  59.  
  60. DEFAULTDB = get_pathname_option('Storage', 'persistent_storage_file')
  61. SPAM_THRESHOLD = options[('Categorization', 'spam_cutoff')]
  62. HAM_THRESHOLD = options[('Categorization', 'ham_cutoff')]
  63.  
  64. def train(h, msgs, is_spam):
  65.     '''Train bayes with all messages from a mailbox.'''
  66.     mbox = mboxutils.getmbox(msgs)
  67.     i = 0
  68.     for msg in mbox:
  69.         i += 1
  70.         if i % 10 == 0:
  71.             sys.stdout.write('\r%6d' % i)
  72.             sys.stdout.flush()
  73.         
  74.         h.train(msg, is_spam)
  75.     
  76.     sys.stdout.write('\r%6d' % i)
  77.     sys.stdout.flush()
  78.     print 
  79.  
  80.  
  81. def untrain(h, msgs, is_spam):
  82.     '''Untrain bayes with all messages from a mailbox.'''
  83.     mbox = mboxutils.getmbox(msgs)
  84.     i = 0
  85.     for msg in mbox:
  86.         i += 1
  87.         if i % 10 == 0:
  88.             sys.stdout.write('\r%6d' % i)
  89.             sys.stdout.flush()
  90.         
  91.         h.untrain(msg, is_spam)
  92.     
  93.     sys.stdout.write('\r%6d' % i)
  94.     sys.stdout.flush()
  95.     print 
  96.  
  97.  
  98. def score(h, msgs, reverse = 0):
  99.     '''Score (judge) all messages from a mailbox.'''
  100.     mbox = mboxutils.getmbox(msgs)
  101.     i = 0
  102.     spams = hams = unsures = 0
  103.     for msg in mbox:
  104.         i += 1
  105.         (prob, clues) = h.score(msg, True)
  106.         if hasattr(msg, '_mh_msgno'):
  107.             msgno = msg._mh_msgno
  108.         else:
  109.             msgno = i
  110.         isspam = prob >= SPAM_THRESHOLD
  111.         isham = prob <= HAM_THRESHOLD
  112.         if isspam:
  113.             spams += 1
  114.             if not reverse:
  115.                 if not isspam or 'S':
  116.                     pass
  117.                 print '%6s %4.2f %1s' % (msgno, prob, '.'), h.formatclues(clues)
  118.             
  119.         reverse
  120.         if isham:
  121.             hams += 1
  122.             if reverse:
  123.                 if not isham or 'S':
  124.                     pass
  125.                 print '%6s %4.2f %1s' % (msgno, prob, '.'), h.formatclues(clues)
  126.             
  127.         reverse
  128.         unsures += 1
  129.         print '%6s %4.2f U' % (msgno, prob), h.formatclues(clues)
  130.     
  131.     return (spams, hams, unsures)
  132.  
  133.  
  134. def usage(code, msg = ''):
  135.     '''Print usage message and sys.exit(code).'''
  136.     if msg:
  137.         print >>sys.stderr, msg
  138.         print >>sys.stderr
  139.     
  140.     print >>sys.stderr, __doc__ % globals()
  141.     sys.exit(code)
  142.  
  143.  
  144. def main():
  145.     '''Main program; parse options and go.'''
  146.     
  147.     try:
  148.         (opts, args) = getopt.getopt(sys.argv[1:], 'hd:Ufg:s:p:u:r')
  149.     except getopt.error:
  150.         msg = None
  151.         usage(2, msg)
  152.  
  153.     if not opts:
  154.         usage(2, 'No options given')
  155.     
  156.     pck = DEFAULTDB
  157.     good = []
  158.     spam = []
  159.     unknown = []
  160.     reverse = 0
  161.     untrain_mode = 0
  162.     do_filter = False
  163.     usedb = None
  164.     mode = 'r'
  165.     for opt, arg in opts:
  166.         if opt == '-h':
  167.             usage(0)
  168.             continue
  169.         if opt == '-g':
  170.             good.append(arg)
  171.             mode = 'c'
  172.             continue
  173.         if opt == '-s':
  174.             spam.append(arg)
  175.             mode = 'c'
  176.             continue
  177.         if opt == '-f':
  178.             do_filter = True
  179.             continue
  180.         if opt == '-u':
  181.             unknown.append(arg)
  182.             continue
  183.         if opt == '-U':
  184.             untrain_mode = 1
  185.             continue
  186.         if opt == '-r':
  187.             reverse = 1
  188.             continue
  189.     
  190.     (pck, usedb) = storage.database_type(opts)
  191.     if args:
  192.         usage(2, 'Positional arguments not allowed')
  193.     
  194.     if usedb == None:
  195.         usage(2, 'Must specify one of -d or -D')
  196.     
  197.     save = False
  198.     h = hammie.open(pck, usedb, mode)
  199.     if not untrain_mode:
  200.         for g in good:
  201.             print 'Training ham (%s):' % g
  202.             train(h, g, False)
  203.             save = True
  204.         
  205.         for s in spam:
  206.             print 'Training spam (%s):' % s
  207.             train(h, s, True)
  208.             save = True
  209.         
  210.     else:
  211.         for g in good:
  212.             print 'Untraining ham (%s):' % g
  213.             untrain(h, g, False)
  214.             save = True
  215.         
  216.         for s in spam:
  217.             print 'Untraining spam (%s):' % s
  218.             untrain(h, s, True)
  219.             save = True
  220.         
  221.     if save:
  222.         h.store()
  223.     
  224.     if do_filter:
  225.         msg = sys.stdin.read()
  226.         filtered = h.filter(msg)
  227.         sys.stdout.write(filtered)
  228.     
  229.     if unknown:
  230.         spams = hams = unsures = 0
  231.         for u in unknown:
  232.             if len(unknown) > 1:
  233.                 print 'Scoring', u
  234.             
  235.             (s, g, u) = score(h, u, reverse)
  236.             spams += s
  237.             hams += g
  238.             unsures += u
  239.         
  240.         print 'Total %d spam, %d ham, %d unsure' % (spams, hams, unsures)
  241.     
  242.  
  243. if __name__ == '__main__':
  244.     main()
  245.  
  246.